package flexUnitTests.testCases
{
	import com.competentum.builders.BindsBuilder;
	import com.competentum.builders.DataBuildersFactory;
	import com.competentum.builders.EmployeesBuilder;
	import com.competentum.builders.PreferencesBuilder;
	import com.competentum.builders.ProjectsBuilder;
	import com.competentum.data.parser.XMLParser;
	import com.competentum.engines.BindsEngine;
	import com.competentum.engines.CommandPanelEngine;
	import com.competentum.engines.EmployeesEngine;
	import com.competentum.engines.EngineFactory;
	import com.competentum.engines.InputOutputEngine;
	import com.competentum.engines.ProjectsEngine;
	import com.competentum.engines.ShortCutsEngine;
	import com.competentum.vos.BindVO;
	import com.competentum.vos.EmployeeVO;
	import com.competentum.vos.ProjectVO;
	import com.competentum.windows.Scope;
	import com.competentum.lib.engines.AbstractEngine;
	import com.competentum.lib.engines.AbstractEngineFactory;
	
	import org.flexunit.asserts.assertEquals;

	public class XMLParserTest
	{
		private var SCOPE:Scope = new Scope(null);
		
		private var parser:XMLParser;
		
		private var xml:XML =
			<root>
			  <employees>
				<employee name="Полуянович" attestation="15" team="teamMinsk" uid="99">
				  <bind toProject="102" hoursReserved="15"/>
				</employee>
				<employee name="Миронова" attestation="50" team="team15" uid="94">
				  <bind toProject="88" hoursReserved="45"/>
				</employee>
				<employee name="Соболев" attestation="40" team="team15" uid="95">
				  <bind toProject="86" hoursReserved="40"/>
				  <bind toProject="88" hoursReserved="20"/>
				</employee>
			  </employees>
			  <projects>
				<project name="EV_CFIN1e" duration="177" uid="102" comments="Project Comments" groupingAttribute="ExamView"/>
				<project name="Mayo" duration="257" uid="86" comments="" groupingAttribute="Cengage"/>
				<project name="CLeBook links" duration="95" uid="88" groupingAttribute="Cengage"/>
			  </projects>
			</root>;
		
		[Before]
		public function setUp():void
		{
			var dataBuildersFactory:DataBuildersFactory = new DataBuildersFactory();
			parser = new XMLParser();
		}
		
		[Test]
		public function test():void
		{
			parser.parse(xml, SCOPE);
			var bindsBuilder:BindsBuilder = DataBuildersFactory.getDataBuilder(BindsBuilder.TYPE, SCOPE) as BindsBuilder;
			var employeesBuilder:EmployeesBuilder = DataBuildersFactory.getDataBuilder(EmployeesBuilder.TYPE, SCOPE) as EmployeesBuilder;
			var employee:EmployeeVO = employeesBuilder.getItemByUid('99') as EmployeeVO;
			assertEquals(employee.name, 'Полуянович');
			assertEquals(employee.hours, 15);
			assertEquals(employee.team, 'teamMinsk');
			assertEquals(bindsBuilder.getTotalReservedHours(employee), 15);
			employee = employeesBuilder.getItemByUid('94') as EmployeeVO;
			assertEquals(employee.name, 'Миронова');
			assertEquals(employee.hours, 50);
			assertEquals(employee.team, 'team15');
			assertEquals(bindsBuilder.getTotalReservedHours(employee), 45);
			employee = employeesBuilder.getItemByUid('95') as EmployeeVO;
			assertEquals(employee.name, 'Соболев');
			assertEquals(employee.hours, 40);
			assertEquals(employee.team, 'team15');
			assertEquals(bindsBuilder.getTotalReservedHours(employee), 60);
			
			var projectsBuilder:ProjectsBuilder = DataBuildersFactory.getDataBuilder(ProjectsBuilder.TYPE, SCOPE) as ProjectsBuilder;
			var project:ProjectVO = projectsBuilder.getItemByUid('102') as ProjectVO;
			assertEquals(project.name, 'EV_CFIN1e');
			assertEquals(project.groupingAttribute, 'ExamView');
			assertEquals(project.hours, 177);
			assertEquals(project.comments, 'Project Comments');
			assertEquals(bindsBuilder.getTotalReservedHours(project), 15);
			project = projectsBuilder.getItemByUid('86') as ProjectVO;
			assertEquals(project.name, 'Mayo');
			assertEquals(project.groupingAttribute, 'Cengage');
			assertEquals(project.hours, 257);
			assertEquals(project.comments, '');
			assertEquals(bindsBuilder.getTotalReservedHours(project), 40);
			project = projectsBuilder.getItemByUid('88') as ProjectVO;
			assertEquals(project.name, 'CLeBook links');
			assertEquals(project.groupingAttribute, 'Cengage');
			assertEquals(project.hours, 95);
			assertEquals(project.comments, '');
			assertEquals(bindsBuilder.getTotalReservedHours(project), 65);
			
			var outXML:XML = new XML(parser.export(SCOPE));
			assertEquals(outXML.metadata[0].@version, (DataBuildersFactory.getDataBuilder(PreferencesBuilder.TYPE, SCOPE) as PreferencesBuilder).VERSION);
			assertEquals(outXML.employees.elements().length(), employeesBuilder.itemsCount);
			assertEquals(outXML.projects.elements().length(), projectsBuilder.itemsCount);

			assertEquals(outXML.employees.employee[0].@uid, '99');
			assertEquals(outXML.employees.employee[0].@name, 'Полуянович');
			assertEquals(outXML.employees.employee[0].@attestation, 15);
			assertEquals(outXML.employees.employee[0].@team, 'teamMinsk');
			assertEquals(outXML.employees.employee[1].@uid, '94');
			assertEquals(outXML.employees.employee[1].@name, 'Миронова');
			assertEquals(outXML.employees.employee[1].@attestation, 50);
			assertEquals(outXML.employees.employee[1].@team, 'team15');
			assertEquals(outXML.employees.employee[2].@uid, '95');
			assertEquals(outXML.employees.employee[2].@name, 'Соболев');
			assertEquals(outXML.employees.employee[2].@attestation, 40);
			assertEquals(outXML.employees.employee[2].@team, 'team15');
			
			assertEquals(outXML.projects.project[0].@uid, '102');
			assertEquals(outXML.projects.project[0].@name, 'EV_CFIN1e');
			assertEquals(outXML.projects.project[0].@groupingAttribute, 'ExamView');
			assertEquals(outXML.projects.project[0].@duration, 177);
			assertEquals(outXML.projects.project[0].@comments, 'Project Comments');
			assertEquals(outXML.projects.project[1].@uid, '86');
			assertEquals(outXML.projects.project[1].@name, 'Mayo');
			assertEquals(outXML.projects.project[1].@groupingAttribute, 'Cengage');
			assertEquals(outXML.projects.project[1].@duration, 257);
			assertEquals(outXML.projects.project[1].@comments, '');
			assertEquals(outXML.projects.project[2].@uid, '88');
			assertEquals(outXML.projects.project[2].@name, 'CLeBook links');
			assertEquals(outXML.projects.project[2].@groupingAttribute, 'Cengage');
			assertEquals(outXML.projects.project[2].@duration, 95);
			assertEquals(outXML.projects.project[2].@comments, '');
			
			assertEquals(outXML.employees.employee[0].bind[0].@toEmployee, '99');
			assertEquals(outXML.employees.employee[0].bind[0].@toProject, '102');
			assertEquals(outXML.employees.employee[0].bind[0].@hoursReserved, 15);
			assertEquals(outXML.employees.employee[1].bind[0].@toEmployee, '94');
			assertEquals(outXML.employees.employee[1].bind[0].@toProject, '88');
			assertEquals(outXML.employees.employee[1].bind[0].@hoursReserved, 45);
			assertEquals(outXML.employees.employee[2].bind[0].@toEmployee, '95');
			assertEquals(outXML.employees.employee[2].bind[0].@toProject, '86');
			assertEquals(outXML.employees.employee[2].bind[0].@hoursReserved, 40);
			assertEquals(outXML.employees.employee[2].bind[1].@toEmployee, '95');
			assertEquals(outXML.employees.employee[2].bind[1].@toProject, '88');
			assertEquals(outXML.employees.employee[2].bind[1].@hoursReserved, 20);
		}
	}
}